//
//  CircleView.m
//  ChartsDemo
//
//  Created by apple on 2021/3/29.
//

#import "CircleView.h"

/* 弧度转角度 */
#define SK_RADIANS_TO_DEGREES(radian) \
        ((radian) * (180.0 / M_PI))
/* 角度转弧度 */
#define SK_DEGREES_TO_RADIANS(angle) \

@interface CircleView ()
{
    UIColor *_defult_bg;
    UIColor *_defult_s;
    UIColor *_defult_e;
    
    UIColor *_red_bg;
    UIColor *_red_s;
    UIColor *_red_e;
    
    UIColor *_orange_bg;
    UIColor *_orange_s;
    UIColor *_orange_e;
}
@property(nonatomic,strong) CAGradientLayer *gradientLayer1;
@property(nonatomic,strong) CAGradientLayer *gradientLayer2;

@property(nonatomic,strong) UIView *bgView;

@end


@implementation CircleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _defult_bg = [UIColor colorWithRed:0/255.0 green:149/255.0 blue:182/255.0 alpha: 0.2];
        _defult_s = [UIColor colorWithRed:0/255.0 green:149/255.0 blue:182/255.0 alpha: 0.4];
        _defult_e = [UIColor colorWithRed:0/255.0 green:149/255.0 blue:182/255.0 alpha:1.0];
        
        _red_bg = [UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha: 0.2];
        _red_s = [UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha: 0.4];
        _red_e = [UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
        
        _orange_bg = [UIColor colorWithRed:255/255.0 green:97/255.0 blue:0/255.0 alpha: 0.2];
        _orange_s = [UIColor colorWithRed:255/255.0 green:97/255.0 blue:0/255.0 alpha: 0.4];
        _orange_e = [UIColor colorWithRed:255/255.0 green:97/255.0 blue:0/255.0 alpha:1.0];
        
        
        
        CGFloat startAngle = -M_PI; // 起始角度
        CGFloat endAngle = -M_PI+M_PI*2; // 结束角度
    
        CAShapeLayer *outLayer=[CAShapeLayer layer];
        outLayer.frame = self.bounds;
        [self.layer addSublayer:outLayer];
        outLayer.fillColor= [UIColor clearColor].CGColor;
        outLayer.strokeColor = [UIColor clearColor].CGColor;
        outLayer.opacity = 1;
        //kCALineCapRound(圆角)，kCALineCapButt（直角
        outLayer.lineCap = kCALineCapButt;
        outLayer.lineWidth = 9;
        
        UIColor *strokeColor = [UIColor colorWithString:@"#F0F1F5"];//背景圆环的颜色
        CAShapeLayer *inLayer = [CAShapeLayer layer];
        inLayer.frame = self.bounds;
        [self.layer addSublayer:inLayer];
        inLayer.fillColor= [UIColor clearColor].CGColor;
        inLayer.strokeColor = strokeColor.CGColor;

        
        inLayer.opacity = 1;
        //kCALineCapRound(圆角)，kCALineCapButt（直角
        inLayer.lineCap = kCALineCapButt;
        inLayer.lineWidth = 22;
        
        UIBezierPath *outPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width/2, frame.size.height/2) radius:frame.size.width/2-6 startAngle:startAngle endAngle:endAngle clockwise:YES];
        outLayer.path = [outPath CGPath];
        _circleLayer = [CAShapeLayer layer];
        _circleLayer.frame = self.bounds;
        _circleLayer.fillColor = [[UIColor clearColor] CGColor];
        _circleLayer.strokeColor = [UIColor whiteColor].CGColor;
        _circleLayer.lineCap = kCALineCapRound;
        _circleLayer.lineWidth = 9;
        _circleLayer.path =[outPath CGPath];
        _circleLayer.strokeEnd = 0.0;
        
        CALayer *layer1 = [CALayer layer];
        self.gradientLayer1 = [CAGradientLayer layer];
        self.gradientLayer1.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
        [self.gradientLayer1 setStartPoint:CGPointMake(0.5, 0)];
        [self.gradientLayer1 setEndPoint:CGPointMake(0.5, 1)];
        [layer1 addSublayer:self.gradientLayer1];
        [layer1 setMask:_circleLayer];
        [self.layer addSublayer:layer1];
        
        UIBezierPath *inPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(frame.size.width/2, frame.size.height/2) radius:frame.size.width/2 - 22 startAngle:startAngle endAngle:endAngle clockwise:YES];
        inLayer.path = [inPath CGPath];
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.frame = self.bounds;
        _progressLayer.fillColor = [[UIColor clearColor] CGColor];
        _progressLayer.strokeColor = [UIColor whiteColor].CGColor;
        _progressLayer.lineCap = kCALineCapButt;
        _progressLayer.lineWidth = 22;
        _progressLayer.path =[inPath CGPath];
        _progressLayer.strokeEnd = 0.0;
        
        CALayer *layer2 = [CALayer layer];
        self.gradientLayer2 = [CAGradientLayer layer];
        self.gradientLayer2.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
        [layer2 addSublayer:self.gradientLayer2];
        [layer2 setMask:_progressLayer];
        [self.layer addSublayer:layer2];
        
        [self addSubViews];
    }
    return self;
}

-(void)setPercent:(CGFloat)percent animated:(BOOL)animated{
    CGFloat testValue = [self.titleLab.text integerValue];
    if ([self.tipLab.text containsString:@"SpO2"]) {
        // 血氧
        if (testValue <= 88) {
            // 红色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
            self.bgView.layer.shadowColor = _red_e.CGColor;
            self.titleLab.textColor = _red_e;
        }else if(testValue <= 94 && testValue>=89){
            // 橙色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
            self.bgView.layer.shadowColor = _orange_e.CGColor;
            self.titleLab.textColor = _orange_e;
        }else{
            // 黑色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
            self.bgView.layer.shadowColor = _defult_e.CGColor;
            self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
        }
    }else{
        //--------
        // 心率
        if (testValue >= 60 && testValue <= 100) {
            // 黑色显示
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
            self.bgView.layer.shadowColor = _defult_e.CGColor;
            self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
        }else if (testValue >= 121 || testValue <= 50){
            // 红色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
            self.bgView.layer.shadowColor = _red_e.CGColor;
            self.titleLab.textColor = _red_e;
        }else{
            // 橙色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
            self.bgView.layer.shadowColor = _orange_e.CGColor;
            self.titleLab.textColor = _orange_e;
        }
        //---------
//        if (testValue <= 60 || testValue > 120) {
//            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
//            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
//            self.bgView.layer.shadowColor = _red_e.CGColor;
//            self.titleLab.textColor = _red_e;
//        }else if(testValue <= 90 && testValue > 60){
//            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
//            self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
//            self.bgView.layer.shadowColor = _defult_e.CGColor;
//            self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
//        }else if(testValue <= 120 && testValue > 90){
//            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
//            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
//            self.bgView.layer.shadowColor = _orange_e.CGColor;
//            self.titleLab.textColor = _orange_e;
//        }
    }
    if (testValue == 0) {
        [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
        self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
        self.bgView.layer.shadowColor = _defult_e.CGColor;
        self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
    }
    
    
    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    _circleLayer.strokeEnd = percent/360.0;
    _progressLayer.strokeEnd = percent/360.0;
    [CATransaction commit];
}

- (void)setPercent:(CGFloat)percent color:(UIColor *)color animated:(BOOL)animated{
    UIColor *bg = [color colorWithAlphaComponent:0.2];
    UIColor *s = [color colorWithAlphaComponent:0.4];
    UIColor *e = [color colorWithAlphaComponent:1.0];
    [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)s.CGColor,(id)e.CGColor, nil]];
    self.gradientLayer2.backgroundColor = bg.CGColor;
    self.bgView.layer.shadowColor = e.CGColor;
    self.titleLab.textColor = e;
    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    _circleLayer.strokeEnd = percent/360.0;
    _progressLayer.strokeEnd = percent/360.0;
    [CATransaction commit];
}

-(void)setSleepPercent:(CGFloat)percent watchModel:(ARTWathDataModel *)model animated:(BOOL)animated{
    CGFloat testValue = [self.titleLab.text integerValue];
    if ([self.tipLab.text containsString:@"SpO2"]) {
        // 血氧
        if (testValue <= 88) {
            // 红色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
            self.bgView.layer.shadowColor = _red_e.CGColor;
            self.titleLab.textColor = _red_e;
        }else if(testValue <= model.lowSpo){
            // 橙色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
            self.bgView.layer.shadowColor = _orange_e.CGColor;
            self.titleLab.textColor = _orange_e;
        }else{
            // 黑色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
            self.bgView.layer.shadowColor = _defult_e.CGColor;
            self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
        }
    }else{
        //--------
        // 心率
        if (testValue <  35) {
            // 红色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
            self.bgView.layer.shadowColor = _red_e.CGColor;
            self.titleLab.textColor = _red_e;
        }else if (testValue < model.lowPr){
            // 橙色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
            self.bgView.layer.shadowColor = _orange_e.CGColor;
            self.titleLab.textColor = _orange_e;
        }else if (testValue <= model.highPr){
            // 黑色显示
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
            self.bgView.layer.shadowColor = _defult_e.CGColor;
            self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
        }else if (testValue <= 120){
            // 橙色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
            self.bgView.layer.shadowColor = _orange_e.CGColor;
            self.titleLab.textColor = _orange_e;
        }else{
            // 红色
            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
            self.bgView.layer.shadowColor = _red_e.CGColor;
            self.titleLab.textColor = _red_e;
        }
//        if (testValue >= 60 && testValue <= 100) {
//            // 黑色显示
//            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
//            self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
//            self.bgView.layer.shadowColor = _defult_e.CGColor;
//            self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
//        }else if (testValue >= 121 || testValue <= 50){
//            // 红色
//            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_red_s.CGColor,(id)_red_e.CGColor, nil]];
//            self.gradientLayer2.backgroundColor = _red_bg.CGColor;
//            self.bgView.layer.shadowColor = _red_e.CGColor;
//            self.titleLab.textColor = _red_e;
//        }else{
//            // 橙色
//            [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_orange_s.CGColor,(id)_orange_e.CGColor, nil]];
//            self.gradientLayer2.backgroundColor = _orange_bg.CGColor;
//            self.bgView.layer.shadowColor = _orange_e.CGColor;
//            self.titleLab.textColor = _orange_e;
//        }
    }
    if (testValue == 0) {
        [self.gradientLayer1 setColors:[NSArray arrayWithObjects:(id)_defult_s.CGColor,(id)_defult_e.CGColor, nil]];
        self.gradientLayer2.backgroundColor = _defult_bg.CGColor;
        self.bgView.layer.shadowColor = _defult_e.CGColor;
        self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
    }
    
    [CATransaction begin];
    [CATransaction setDisableActions:!animated];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [CATransaction setAnimationDuration:1];
    _circleLayer.strokeEnd = percent/360.0;
    _progressLayer.strokeEnd = percent/360.0;
    [CATransaction commit];
}

- (void)addSubViews{
    
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.frame = CGRectMake(20, 20, self.frame.size.width-40, self.frame.size.width-40);
    [self addSubview:self.bgView];
    CGFloat bgView_W = self.frame.size.width - 62;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.height.mas_equalTo(bgView_W);
    }];
    self.bgView.layer.cornerRadius = bgView_W/2;
    UIColor *bgColor = [UIColor colorWithRed:0/255.0 green:149/255.0 blue:182/255.0 alpha:1.0];
    self.bgView.layer.shadowColor = bgColor.CGColor;
    self.bgView.layer.shadowOffset = CGSizeMake(0,0);
    self.bgView.layer.shadowOpacity = 0.5;
    self.bgView.layer.shadowRadius = 15.0;
    
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.text = @"0";
    self.titleLab.textColor = [UIColor colorWithString:@"#222222"];
    self.titleLab.font = [UIFont systemFontOfSize:30 weight:UIFontWeightBold];
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self.mas_centerY).offset(10);
    }];
    
    self.tipLab = [[UILabel alloc] init];
    self.tipLab.text = FYLocalizedString(@"oxygen_spo2", @"");
    self.tipLab.textColor = [UIColor colorWithString:@"#999999"];
    self.tipLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.tipLab];
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.titleLab.mas_bottom).offset(2);
    }];
    
    self.leftLab = [[UILabel alloc] init];
    self.leftLab.textColor = [UIColor colorWithString:@"#9a9a9a"];
    self.leftLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.leftLab];
    [self.leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-15);
        make.left.equalTo(self).offset(0);
    }];
    
    self.rightLab = [[UILabel alloc] init];
    self.rightLab.textColor = [UIColor colorWithString:@"#9a9a9a"];
    self.rightLab.font = [UIFont systemFontOfSize:12];
    [self addSubview:self.rightLab];
    [self.rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.leftLab);
        make.right.equalTo(self).offset(0);
    }];
}

// 调整标题居中
- (void)adjustTitleLabCenter{
    [self.titleLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
    }];
}

+ (UIColor *)spoColor:(NSInteger)testValue{
    // 血氧
    if (testValue <= 88) {
        // 红色
        return [UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    }else if(testValue <= 94 && testValue>=89){
        // 橙色
        return [UIColor colorWithRed:255/255.0 green:97/255.0 blue:0/255.0 alpha:1.0];
    }else{
        // 黑色
        return [UIColor colorWithString:@"#222222"];
    }
}

+ (UIColor *)prColor:(NSInteger)testValue{
    // 心率
    if (testValue >= 60 && testValue <= 100) {
        // 黑色显示
        return [UIColor colorWithString:@"#222222"];
    }else if (testValue >= 121 || testValue <= 50){
        // 红色
        return [UIColor colorWithRed:255/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
    }else{
        // 橙色
        return [UIColor colorWithRed:255/255.0 green:97/255.0 blue:0/255.0 alpha:1.0];
    }
}

@end
